package com.itheima.info.manage.system.controller;

import com.itheima.info.manage.system.domain.Teacher;
import com.itheima.info.manage.system.domain.Teacher;
import com.itheima.info.manage.system.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController {
    private TeacherService service= new TeacherService();
    private Scanner sc= new Scanner(System.in);
    public void start() {//this
        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            //展示菜单
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            //录入数据
            String choice = sc.nextLine();
            //switch 选择
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("退出学生,回到主界面");
                    // break loop;
                    return;

                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }
    //增 
    public void addTeacher(){
        //录入信息
        String sid=null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exist = service.isExist(sid);
            if(!exist){
                break;
            }else {
                System.out.println("你输入的工号存在,请重新输入");
            }
        }
        Teacher teacherInfo = getTeacherInfo(sid);
        service.addTeacher(teacherInfo);
        System.out.println("添加成功");
    }
    
    //删 
    public void deleteTeacher(){
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据,请先添加");
            return;
        }
        //录入学号
        String sid = getTeacherSid();
        service.deleteTeacherBySid(sid);
        System.out.println("删除成功");
    }
    
    //改 
    public void updateTeacher(){
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据,请先添加");
            return;
        }
        //录入学号
        String sid = getTeacherSid();
        //录入信息
        Teacher teacherInfo = getTeacherInfo(sid);
        service.updateTeacher(sid,teacherInfo);
        System.out.println("修改成功");
    }
    
    //查 
    public void findAllTeachers(){
        //调用service  查询
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        //遍历数组  展示数据
        for (int i = 0; i < list.size(); i++) {
            Teacher stu = list.get(i);
            if (stu != null) {
                System.out.println(stu.getSid() + "\t" + stu.getName()
                        + "\t" + stu.getAge() + "岁\t" + stu.getBirthday());
            }
        }
    }
    
    //录入学号的方法
    public String  getTeacherSid(){
        String sid=null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exist = service.isExist(sid);
            if(exist){
               break;
            }else {
                System.out.println("你输入的工号不存在,请重新输入");
            }
        }
        return sid;
    }
    //录入信息的方法
    
    public Teacher getTeacherInfo(String sid){
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);

        return teacher;
    }
}
